namespace Agile.Common
{
    /// <summary>
    /// Essentially an extended 'bool' for when you want to know
    /// details of why something failed.
    /// </summary>
    /// <example>
    /// public ActionResult Validate()
    /// {
    ///		if(RequiredField == null)
    ///		{
    ///			return ActionResult.Failed("RequiredField is mandatory");
    ///		}
    ///		return ActionResult.Success;
    /// }
    /// </example>
    public class ActionResult
    {
        /// <summary>
        /// The reason why the action failed.
        /// </summary>
        private readonly string _failureReason;

        /// <summary>
        /// Action result constructor.
        /// </summary>
        /// <param name="reason">Reason for failure. Set to null when the action was successful.</param>
        private ActionResult(string reason)
        {
            _failureReason = reason;
        }

        /// <summary>
        /// Instantiate a successful action result.
        /// </summary>
        public static ActionResult Success
        {
            get { return new ActionResult(null); }
        }

        /// <summary>
        /// Gets the reason why the action failed.
        /// </summary>
        public string FailureReason
        {
            get { return _failureReason; }
        }

        /// <summary>
        /// Returns true if the action was successful.
        /// </summary>
        public bool WasSuccessful
        {
            get { return _failureReason == null; }
        }

        /// <summary>
        /// Instantiate a failed action result with the reason for the failure.
        /// </summary>
        /// <param name="reason">Why the action failed.</param>
        /// <returns></returns>
        public static ActionResult Failed(string reason)
        {
            return new ActionResult(reason);
        }
    }
}